package com.ss.android.newmedia.feedback;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.bytedance.common.utility.Logger;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class f {
    static final class a extends SQLiteOpenHelper {
        public a(Context arg4) {
            super(arg4, "feedback.db", null, 2);
        }

        public final void onCreate(SQLiteDatabase arg2) {
            arg2.execSQL("CREATE TABLE feedback (auto_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "item_id INTEGER NOT NULL," +
                    "timestamp INTEGER NOT NULL DEFAULT 0,content VARCHAR," +
                    "image_url VARCHAR," +
                    "avatar_url VARCHAR," +
                    "image_width INTEGER NOT NULL DEFAULT 0," +
                    "image_height INTEGER NOT NULL DEFAULT 0," +
                    "type INTEGER NOT NULL DEFAULT 0," +
                    "links VARCHAR )");
        }

        public final void onUpgrade(SQLiteDatabase arg5, int arg6, int arg7) {
            if(arg6 < 2) {
                try {
                    arg5.execSQL("ALTER TABLE feedback ADD COLUMN links VARCHAR");
                }
                catch(Exception v0) {
                    Logger.d("FeedbackDBManager", "update db to version 2 error:" + v0);
                }
            }
        }
    }

    private final Context a;
    private SQLiteDatabase b;
    private volatile boolean c;
    private static f d;
    private static Object e;
    private static final String[] f;

    static {
        d = null;
        e = new Object();
        f = new String[]{"item_id", "timestamp", "content", "image_url", "avatar_url", "image_width", "image_height", "type", "links"};
    }

    private f(Context arg2) {

        this.c = false;
        this.a = arg2;
    }

    public final long a(boolean arg13) {
        Cursor v1_1;
        Cursor v2_1;
        long v10 = -1;
        Cursor v9 = null;
        synchronized (this) {
            long v0_1;
            try {
                if (this.c()) {
                    String[] v2 = new String[]{"item_id"};
                    StringBuilder v1 = new StringBuilder("item_id");
                    String v0_3 = arg13 ? " DESC" : " ASC";
                    v2_1 = this.b.query("feedback", v2, "type < 2", null, null, null, v1.append(v0_3).toString(), "1");
                    if (v2_1 == null) {
                        return -1L;
                    }
                    if (v2_1.moveToNext()) {
                        v0_1 = v2_1.getLong(0);
                        if (v2_1 == null) {
                            return -1;
                        }
                        return v0_1;
                    }
                    return -1L;
                }
            } catch (Throwable v0) {
                Logger.w("FeedbackDBManager", "get max min id error:" + v0);
            } finally {
                if (v9 != null) {
                    try {
                        v9.close();
                    } catch (Throwable v0) {
                    }
                }
            }
            return v10;
        }
    }

    public static f a(Context arg3) {
        Object v1 = e;
        synchronized (v1){
            if(d == null) {
                d = new f(arg3.getApplicationContext());
            }
        }
        return d;
    }

    private static g a(Cursor arg4) {
        g v0 = new g(arg4.getLong(0));
        v0.b = arg4.getLong(1);
        v0.c = arg4.getString(2);
        v0.d = arg4.getString(3);
        v0.e = arg4.getString(4);
        v0.f = arg4.getInt(5);
        v0.g = arg4.getInt(6);
        v0.h = arg4.getInt(7);
        v0.i = arg4.getString(8);
        return v0;
    }

    public final List a(long arg16, int arg18, String arg19) {
        Cursor v3 = null;
        ArrayList v11 = null;
        synchronized (this) {
            try {
                v11 = new ArrayList();
                if (this.c()) {
                    Cursor v12 = null;
                    try {
                        StringBuffer v5 = new StringBuffer();
                        if (0 > 0) {
                            v5.append("item_id<0");
                        }

                        if (arg16 > 0) {
                            if (v5.length() > 0) {
                                v5.append(" AND ");
                            }
                            v5.append("item_id>" + arg16);
                        }

                        if (v5.length() > 0) {
                            v5.append(" AND ");
                        }

                        v5.append("type < 2");
                        String v10 = null;
                        if (arg18 > 0) {
                            v10 = String.valueOf(arg18);
                        }

                        if (android.support.design.a.isTextEmpty(arg19)) {
                            arg19 = " ASC";
                        }
                        v3 = this.b.query("feedback", f, v5.toString(), null, null, null, "item_id" + arg19, v10);
                        if (v3 != null) {
                            while (v3.moveToNext()) {
                                g v2_3 = a(v3);
                                v2_3.a();
                                ((List) v11).add(v2_3);
                                continue;
                            }
                        }
                    } catch (Exception v2_2) {
                        Logger.w("FeedbackDBManager", "get feedback list e:" + v2_2);
                    } finally {
                        if (v3 != null) {
                            try {
                                v3.close();
                            } catch (Exception e) {

                            }
                        }
                    }
                }
            } catch (Throwable v2) {
            }
            return v11;
        }

    }

    public final void a() {
        synchronized (this) {
            if (this.c()) {
                try {
                    this.b.delete("feedback", null, null);
                } catch (Exception v0_1) {
                    Logger.w("FeedbackDBManager", "clear data e:" + v0_1);
                }
            }
        }
    }

    public final void a(List arg9) {
        try {
            if(!this.c()) {
            }else if(arg9.size() > 0) {
                this.b.beginTransaction();
                Iterator v1 = arg9.iterator();
                while(v1.hasNext()) {
                    Object v0_2 = v1.next();
                    ContentValues v2 = new ContentValues();
                    v2.put("item_id", Long.valueOf(((g)v0_2).a));
                    v2.put("timestamp", Long.valueOf(((g)v0_2).b));
                    v2.put("content", ((g)v0_2).c);
                    v2.put("image_url", ((g)v0_2).d);
                    v2.put("avatar_url", ((g)v0_2).e);
                    v2.put("image_width", Integer.valueOf(((g)v0_2).f));
                    v2.put("image_height", Integer.valueOf(((g)v0_2).g));
                    v2.put("type", Integer.valueOf(((g)v0_2).h));
                    v2.put("links", ((g)v0_2).i);
                    if(this.b.update("feedback", v2, "item_id=?", new String[]{String.valueOf(((g)v0_2).a)}) > 0) {
                        continue;
                    }
                    try {
                        this.b.insert("feedback", null, v2);
                    }catch (Exception e){
                        Logger.w("FeedbackDBManager", "insert feedback item e:" + e);
                    }
                }

                this.b.setTransactionSuccessful();
                this.b.endTransaction();
            }
        } catch(Exception v0_1) {

        }finally {

        }
    }

    private static SQLiteDatabase b(Context arg1) {
        SQLiteDatabase v0_1;
        try {
            v0_1 = new a(arg1).getWritableDatabase();
        }
        catch(Throwable v0) {
            v0_1 = null;
        }

        return v0_1;
    }

    public synchronized final List b() {
        Cursor v1;
        ArrayList v8;
        Cursor v9 = null;
        v8 = new ArrayList();
        if (this.c()) {

        } else {
            return v8;
        }

        v1 = this.b.query("feedback", f, "type == 2", null, null, null, null);
        try {
            while (true) {
                if (v1 != null) {

                    if (v1.moveToNext()) {
                        ((List) v8).add(a(v1));
                        continue;
                    }
                    return v8;


                }
            }
        } catch (Exception v0_2) {
            Logger.w("FeedbackDBManager", "get tip item error=" + v0_2);
        } finally {
            if (v1 != null) {
                try {
                    v1.close();
                } catch (Exception e) {

                }
            }
        }
        return v8;
    }

    private boolean c() {
        boolean v0 = false;
        if (!this.c) {
            if (this.b == null) {
                this.b = b(this.a);
            }
            if (this.b != null && (this.b.isOpen())) {
                return true;
            }
            Logger.w("FeedbackDBManager", "db not establish and open");
        }

        return v0;
    }
}

